package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.TbSkuClassDao;
import com.itheima.entity.TbSkuClass;
import com.itheima.entity.vo.PageVo;
import com.itheima.service.TbSkuClassService;
import com.itheima.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Li
 * @description 针对表【tb_sku_class(商品类别表)】的数据库操作Service实现
 * @createDate 2022-08-25 20:41:14
 */
@Service
public class TbSkuClassServiceImpl extends ServiceImpl<TbSkuClassDao, com.itheima.entity.TbSkuClass> implements TbSkuClassService {

    @Autowired
    TbSkuClassDao tbSkuClassDao;

    @Override
    public com.itheima.entity.TbSkuClass findById(Integer classId) {
        return tbSkuClassDao.selectById(classId);
    }

    @Override
    public PageVo<TbSkuClass> selectPage(Long pageIndex, Long pageSize, String className) {
        Page<TbSkuClass> classPage = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<TbSkuClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(className != null, TbSkuClass::getClassName, className);

        this.page(classPage, wrapper);

        List<TbSkuClass> records = classPage.getRecords();
        PageVo<TbSkuClass> pageVo = new PageVo<>();
        pageVo.setPageIndex(pageIndex);
        pageVo.setPageSize(pageSize);
        pageVo.setTotalPage(classPage.getTotal());
        pageVo.setCurrentPageRecords(records);
        return pageVo;
    }

    @Override
    public TbSkuClass addStuClass(TbSkuClass tbSkuClass) {
        LambdaQueryWrapper<TbSkuClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tbSkuClass.getClassName() != null, TbSkuClass::getClassName, tbSkuClass.getClassName());
        TbSkuClass selectOne = tbSkuClassDao.selectOne(queryWrapper);
        if (selectOne != null) {
            throw new BusinessException("添加失败，商品类型重复");
        }
        this.save(tbSkuClass);
        return tbSkuClass;
    }


}
